package com.task.core.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.ObjectUtils;

import com.task.common.utils.StringUtils;
import com.task.core.domain.info.ExcelFileImportInfo;

public class ExcelReader {

  /**
   * 读取xlsx类型表格
   * @param excel 文件
   * @return
   */
  public static ExcelFileImportInfo readExcelXlsx(File excel) {
    List<String> header = new ArrayList<>();
    List<Map<String, Object>> data = new ArrayList<>();
    try (FileInputStream file = new FileInputStream(excel);
      Workbook workbook = new XSSFWorkbook(file)) {
      Sheet sheet = workbook.getSheetAt(0);
      Row headerRow = sheet.getRow(0);
      for (Cell cell : headerRow) {
        header.add(cell.getStringCellValue());
      }
      for (int i = 1; i <= sheet.getLastRowNum(); i++) {
        Row dataRow = sheet.getRow(i);
        Map<String, Object> rowData = new HashMap<>();
        for (int j = 0; j < header.size(); j++) {
          Cell dataCell = dataRow.getCell(j);
          switch (dataCell.getCellType()) {
            case STRING:
              rowData.put(header.get(j), dataCell.getStringCellValue());
              break;
            case NUMERIC:
              rowData.put(header.get(j), dataCell.getNumericCellValue());
              break;
            case BOOLEAN:
              rowData.put(header.get(j), dataCell.getBooleanCellValue());
              break;
            case FORMULA:
              rowData.put(header.get(j), dataCell.getCellFormula());
              break;
            default:
              break;
          }
        }
        data.add(rowData);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    return new ExcelFileImportInfo(header, data);
  }

  /**
   * 读取csv类型文件
   * @param source 文件路径
   * @return
   */
  public static ExcelFileImportInfo readExcelCsv(String source) {
    List<String> header = new ArrayList<>();
    List<Map<String, Object>> data = new ArrayList<>();
    try {
      FileReader fileReader = new FileReader(source);
      CSVParser csvParser = new CSVParser(fileReader, CSVFormat.DEFAULT);
      int i=0;
      for (CSVRecord csvRecord : csvParser) {
        if (i==0) {
          //第一行,标题
          for (String field : csvRecord) {
            if (StringUtils.isEmpty(field) || StringUtils.isEmpty(field.trim())) {
              continue;
            }
            header.add(field.toLowerCase());
          }
        }else {
          Map<String, Object> rowData = new HashMap<>();
          //内容,根据标题列数遍历,不然可能会对不齐
          boolean empty = true;
          for (int j = 0; j < header.size(); j++) {
            rowData.put(header.get(j), csvRecord.get(j));
            //判断是不是全部为空
            if (empty && !ObjectUtils.isEmpty(csvRecord.get(j))) {
              empty = false;
            }
          }
          if (!empty) {
            data.add(rowData);
          }
        }
        i++;
      }
      csvParser.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return new ExcelFileImportInfo(header, data);
  }
}